package com.funcoes.android;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Vibrator;

public class GPSFacade implements LocationListener {
    
    //private static final long MINUTE = 60000;
    private static final long INTERVAL_CAPTURE_IN_MILIS = 180000;
     
    private static final float INTERVAL_PHYSICAL_SPACE_IN_METERS = 30;
     
     
    private Context context;
    private LocationManager locationManager;// = (LocationManager) context.getSystemService(LOCATION_SERVICE);
     
    private List<Location> listLocation = new ArrayList<Location>();
    
    Location location;
     
    private static GPSFacade instance;
    int bla = 0;
     
    private boolean capturingGPSStarted = false;
     
     
    private GPSFacade(Context context) {
        this.context = context;
        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    }
     
     
    public static GPSFacade getInstance(Context context) {
        if(instance == null) 
            instance = new GPSFacade(context);
         
        return instance;
    }
     
     
    public boolean isGPSEnable() {
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
     
     
    public void startCaptureLocation() {
        //capturingGPSStarted = true;
     	
    	
             locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, INTERVAL_CAPTURE_IN_MILIS, INTERVAL_PHYSICAL_SPACE_IN_METERS, this);
        	 //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, INTERVAL_CAPTURE_IN_MILIS, INTERVAL_PHYSICAL_SPACE_IN_METERS, this);
        	 location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        	 //Log.d("GPS Enabled", "GPS Enabled");
        	 listLocation.add(location);    
             
        	 //bla++;
   }
     
    public void stopCaptureLocation() {
        capturingGPSStarted = false;
        locationManager.removeUpdates(this);
    }
     
    public List<Location> getListLocation() {
        return listLocation;
    }
     
    public boolean isCapturingGPSStarted() {
        return capturingGPSStarted;
    }
     
    public Location getLastLocation() {
        
        return location;
    }
 
    @Override
    public void onLocationChanged(Location location) {
        listLocation.add(location); 
        vibrar();
    }
     
    private void vibrar() {
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(300);
    }
 
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
 
}